package tools.utils {
	import com.greensock.easing.Elastic;
	import com.greensock.plugins.TransformAroundCenterPlugin;
	import com.greensock.plugins.TweenPlugin;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.geom.Rectangle;
	import flash.printing.PrintJob;

	import com.greensock.TweenLite;

	import flash.display.Sprite;

	import tools.string.Sprintf;

	import flash.display.DisplayObject;

	/**
	 * @author ase7en
	 * @email as@ase7en.com
	 * @email www.ase7en.com/blog	
	 * @date 2009-9-3 1:17
	 */
	public class DisplayUtil {
		private static var _scaleCenter : Boolean;
		private static var _currentObj : DisplayObject;
		private static var _isFlash : Boolean;
		private static var _flashDelay:Number;

		public function DisplayUtil() {
			// TweenPlugin.activate([TransformAroundCenterPlugin]);
		}
		
		public static function setPosition(displayObj:DisplayObject, dx:Number, dy:Number):void
		{
			displayObj.x = dx - displayObj.getBounds(displayObj).left;
			displayObj.y = dy - displayObj.getBounds(displayObj).top;
		}
		
		//set display to level 
		//level is "top" or "bottom"
		public static function setLevel(displayObj:DisplayObject,level:String):void
		{
			
			var parentDisObj:DisplayObjectContainer = displayObj.parent;
			if (level == "top") {
			    parentDisObj.addChild(displayObj);
				
				
			}else if (level == "bottom") {
				parentDisObj.addChildAt(displayObj,0);
			}
		}
		

		// 激活居中缩放插件
		private static function active() : void {
			_scaleCenter = true;
			TweenPlugin.activate([TransformAroundCenterPlugin]);
		}
		
		public static function easeScale(disObj : DisplayObject, delay : Number, scale : Number) : void {
		if (!_scaleCenter) {
				active();
			}
			TweenLite.to(disObj, delay, {transformAroundCenter:{scaleX:scale, scaleY:scale}, ease:Elastic.easeInOut});
		}

		// 缩放显示
		public static function scaleShow(disObj : DisplayObject) : void {
			if (!_scaleCenter) {
				active();
			}
			disObj.visible = true;
			TweenLite.to(disObj, 1, {transformAroundCenter:{scaleX:1, scaleY:1}, ease:Elastic.easeInOut});
		}

		// 缩放隐藏
		public static function scaleHide(disObj : DisplayObject) : void {
			if (!_scaleCenter) {
				active();
			}
			TweenLite.to(disObj, 1, {transformAroundCenter:{scaleX:0, scaleY:0}, ease:Elastic.easeInOut, onComplete:endHide, onCompleteParams:[disObj]});
		}

		// 渐隐
		public static function show(disObj : DisplayObject,delay:Number = 1) : void {
			_currentObj = disObj;
			disObj.alpha = 0;
			disObj.visible = true;
			TweenLite.to(disObj, delay, {alpha:1, onComplete:endShow, onCompleteParams:[disObj]});
		}

		// 渐显
		public static function hide(disObj : *,delay:Number = 1,isVisible:Boolean = false) : void {
			_currentObj = disObj;
			try {
				disObj.mouseChildren = false;
			}catch (e:Error) {
				
			}
			
			TweenLite.to(disObj, delay, {alpha:0, onComplete:endHide, onCompleteParams:[disObj,isVisible]});
		}

		private static function endHide(disObj : DisplayObject,isVisible:Boolean) : void {
			if(!isVisible){
			    disObj.visible = false;
			}
			
			if (_isFlash) {
				
				show(disObj,_flashDelay);
			}
		}

		private static function endShow(disObj : * ) : void {
			try {
				disObj.mouseChildren = true;
			}catch (e:Error) {
				
			}
			
			if (_isFlash) {
				hide(disObj,_flashDelay);
			}
		}

		public static function flashing(disobj : *,delay:Number = 0.5) : void {
			hide(disobj);
			_flashDelay = delay
			_isFlash = true;
		}

		public static function dispose(disobj : * , isShow : Boolean) : void {
			_isFlash = false;
			TweenLite.killTweensOf(disobj);
			if (_currentObj) {
				if (isShow) {
					_currentObj.visible = true;
					_currentObj.alpha = 1;
				} else {
					_currentObj.visible = false;
					_currentObj.alpha = 0;
				}
			}
			
		}
		
		//移除所有对象
		public static function removeAllChilds(container:DisplayObjectContainer):void
		{
			while (container.numChildren > 0) {
				container.removeChildAt(0);
			}
		}
		
		//获取子对象中W最大的对象的width
		public static function getMaxChildWidth(container:DisplayObjectContainer):Number
		{
			var maxW:Number = 0;
			for (var i:int = 0; i < container.numChildren; i++) 
			{
				var item: DisplayObject = container.getChildAt(i) as DisplayObject;
				maxW = Math.max(maxW, item.width);
			}
			return maxW;
		}
		//获取子对象中H最大的对象的height
		public static function getMaxChildHeight(container:DisplayObjectContainer):Number
		{
			var maxH:Number = 0;
			for (var i:int = 0; i < container.numChildren; i++) 
			{
				var item: DisplayObject = container.getChildAt(i) as DisplayObject;
				maxH = Math.max(maxH, item.height);
			}
			return maxH;
		}
		
		//将对象置于容器的正中心位置
		public static function centerDiplayObj(displayObj:DisplayObject):void
		{
			if (displayObj.parent) {
				var parentObj:DisplayObjectContainer = displayObj.parent;
				displayObj.x = (parentObj.width - displayObj.width) / 2;
				displayObj.y = (parentObj.height - displayObj.height) / 2;
			}
		}
		
		//创建遮罩元件
		public static function createMaskObj(rectangle:Rectangle):Shape
		{
			var maskObj:Shape = new Shape();
			maskObj.graphics.beginFill(0);
			maskObj.graphics.drawRect(0, 0, rectangle.width, rectangle.height);
			maskObj.graphics.endFill();
			maskObj.x = rectangle.x;
			maskObj.y = rectangle.y;
			return maskObj;
		}
	}
}